'''

Case Type： 数据库系统
Case Name： gs_ctl stop 使用smart方式在主机停止集群
Create At:  2020/07/25
@zou_jialiang0505328126
Descption:  1. 执行TPCC2.执行TPCC过程中stop主机3.重启数据库4.循环步骤1.2.3 10次5.查询数据库状态
6.执行TPCC，检查数据库是否可正常使用
history：
created： 2020/07/25  created by @wan005
'''
import os
import unittest
from yat.test import Node
import time
from yat.test import macro
import sys

sys.path.append(sys.path[0] + "/../")
from testcase.utils.Logger import Logger
from testcase.utils.Constant import Constant
from testcase.utils.Common import Common
from testcase.utils.CommonSH import *

logger = Logger()
common = Common()


class Stop_immediate(unittest.TestCase):
    dbPrimaryUserNode = Node(node='PrimaryDbUser')
    dbPrimaryRootNode = Node(node='PrimaryRoot')
    DB_INSTANCE_PATH = macro.DB_INSTANCE_PATH
    DB_ENV_PATH = macro.DB_ENV_PATH
    UNRECOGNIZED_SHUTDOWN_MSG = Constant.UNRECOGNIZED_SHUTDOWN_MSG
    STANDBY_NORMAL = Constant.STANDBY_NORMAL
    PRIMARY_NORMAL = Constant.PRIMARY_NORMAL
    START_SUCCESS_MSG = Constant.START_SUCCESS_MSG
    TPCC_PATH = macro.TPCC_PATH
    TPCC_ERROR = Constant.TPCC_ERROR
    TPCC_SUCCESS_MSG = Constant.TPCC_SUCCESS_MSG
    commsh = CommonSH()
    nodes_tuple = ('PrimaryDbUser', 'Standby1DbUser', 'Standby2DbUser')

    @RestartDbCluster(*nodes_tuple)
    def setUp(self):
        logger.info("-----------this is setup-----------")
        logger.info('------------Opengauss_Reliability_Dbsys_Case125.py start------------')

    def test_stop_immediate(self):
        for i in range(1, 2):
            logger.info('------------------round ' + str(i) + ' ------------------')

            logger.info("-----------stop opengauss smart-----------")
            stopCmd = '''
                source {source_path};
                gs_ctl stop -D {DN1_PATH} -m smart'''.format(source_path=self.DB_ENV_PATH,
                                                             DN1_PATH=self.DB_INSTANCE_PATH)
            stopMsg = self.dbPrimaryUserNode.sh(stopCmd).result()
            logger.info(stopMsg)
            self.assertTrue(stopMsg.find(self.UNRECOGNIZED_SHUTDOWN_MSG) > -1)

        logger.info('--------------------check opengauss status-------------------------')
        check_cmd = '''
            source {source_path};
            gs_om -t status --detail'''.format(source_path=self.DB_ENV_PATH)
        check_msg = self.dbPrimaryUserNode.sh(check_cmd).result()
        self.assertTrue(check_msg.find(self.STANDBY_NORMAL) > -1)
        self.assertTrue(check_msg.find(self.PRIMARY_NORMAL) > -1)

        logger.info('--------------------start TPCC-------------------------')
        tpcc_result = common.startTPCC(self.dbPrimaryRootNode, self.TPCC_PATH)
        self.assertTrue(tpcc_result.find(self.TPCC_SUCCESS_MSG) > -1)
        logger.info('-------------' + tpcc_result + '-----------------------')

    def tearDown(self):
        logger.info('----------------this is tearDown-----------------------')
        logger.info('--------------------start opengauss-------------------------')
        start_cmd = '''
            source {source_path};
            gs_om -t start
            '''.format(source_path=self.DB_ENV_PATH)
        startMsg = self.dbPrimaryUserNode.sh(start_cmd).result()
        logger.info(startMsg)
